pacman::p_load(tidyverse,xtable)

setwd("")#set working directory 
nh.2010.gop <- read.csv("nh_2010_gop_032424_GB.csv") %>%
  .[1:143,1:24] %>%
  .[c(-43),] #NOTE: Date says 2020; coding mistake, exclude.
pa.2016.dems <- read.csv("pa_2016_dems_041124_GB.csv") %>%
  .[1:95,]
pa.2022.gop <- read.csv("pa_2022_gop_032424_GB.csv") %>%
  .[1:146,]
pa.2022.dems <- read.csv("pa_2022_dems_032424_GB.csv") %>%
  .[1:82,]
oh.2018.gop <- read.csv("oh_2018_gop_041124_GB.csv") %>%
  .[1:99,]
al.2022.gop <- read.csv("al_2022_gop_052924_GB.csv")
al.2020.gop <- read.csv("al_2020_gop_052924_GB.csv")
al.2017.gop <- read.csv("al_2017_gop_052924_GB.csv") %>%
  .[1:159,]

# table A28 ---- 

### gop 2010
colnames(nh.2010.gop) <- gsub("\\.", "_", colnames(nh.2010.gop))
colnames(nh.2010.gop) <- gsub("__", "_", colnames(nh.2010.gop))
colnames(nh.2010.gop) <- gsub("Center_right_", "Center Right_", colnames(nh.2010.gop))
colnames(nh.2010.gop) <- gsub("Far_Right_", "Far Right_", colnames(nh.2010.gop))
nh.2010.gop.reshaped <- pivot_longer(nh.2010.gop, cols = -c(Source, Date, Title), names_to = "Adjective", values_to = "Value")
nh.2010.gop.reshaped1 <- separate(nh.2010.gop.reshaped, col = c("Adjective"), into = c("Candidate", "Adjective", "Empty"), sep = "_") %>%
  dplyr::select(-Empty) %>%
  mutate(Date = as.Date(Date, format = "%d/%m/%Y"))

#discerning negative vs. affirmative mentions (originally 2 was used to code negative mentions, e.g., "not establishment")
for (i in 1:length(nh.2010.gop.reshaped1$Value)){
  if (nh.2010.gop.reshaped1$Value[i] == 2){
    nh.2010.gop.reshaped1$Adjective[i] <- paste0("Not ", nh.2010.gop.reshaped1$Adjective[i])
    nh.2010.gop.reshaped1$Value[i] <- 1
  }
}

#reshape dataframe
nh.2010.gop.reshaped2.newspaper <- nh.2010.gop.reshaped1 %>%
  mutate(local = ifelse(Source%in%c("Union Leader"), "Local", "National"))%>%
  group_by(Adjective, Candidate) %>%
  dplyr::summarize(total = sum(Value)) %>%
  spread(key = Adjective, value = total) %>%
  replace(is.na(.), 0) %>%
  column_to_rownames(var = "Candidate") %>%
  as.data.frame(.)

#get percentages
nh.2010.gop.reshaped2.newspaper[1,] <- nh.2010.gop.reshaped2.newspaper[1,] / rowSums(nh.2010.gop.reshaped2.newspaper)[1]
nh.2010.gop.reshaped2.newspaper[2,] <- nh.2010.gop.reshaped2.newspaper[2,] / rowSums(nh.2010.gop.reshaped2.newspaper)[2]
nh.2010.gop.reshaped2.newspaper[3,] <- nh.2010.gop.reshaped2.newspaper[3,] / rowSums(nh.2010.gop.reshaped2.newspaper)[3]
for (i in 1:length(nh.2010.gop.reshaped2.newspaper)){
  nh.2010.gop.reshaped2.newspaper[,i] <- nh.2010.gop.reshaped2.newspaper[,i] * 100
}

print(xtable(t(nh.2010.gop.reshaped2.newspaper),digits=0))#table A28

print(xtable(t(nh.2010.gop.reshaped2.newspaper),
             digits=0,
             label="t:newspaper-nh-2010", 
             caption="Distribution of adjectives used to describe New Hampshire’s top candidates for the Republican nomination to the Senate in 2010 in 142 articles from local (Union Leader ) and national (the Associated Press and Politico) outlets. Percentages reflect fraction of all adjectives used to describe each candidate"),
      file="tableA28.tex")

# table A29 ---- 
colnames(pa.2016.dems) <- gsub("\\.", "_", colnames(pa.2016.dems))
colnames(pa.2016.dems) <- gsub("__", "_", colnames(pa.2016.dems))
colnames(pa.2016.dems) <- gsub("McGinty__", "McGinty_", colnames(pa.2016.dems))
colnames(pa.2016.dems) <- gsub("Center_Left_", "Center Left_", colnames(pa.2016.dems))
colnames(pa.2016.dems) <- gsub("Far_Left_", "Far Left_", colnames(pa.2016.dems))
pa.2016.dems.reshaped <- pivot_longer(pa.2016.dems, cols = -c(Source, Date, Title), names_to = "Adjective", values_to = "Value")
pa.2016.dems.reshaped1 <- separate(pa.2016.dems.reshaped, col = c("Adjective"), into = c("Candidate", "Adjective", "Empty"), sep = "_") %>%
  dplyr::select(-Empty) %>%
  mutate(Date = as.Date(Date, format = "%d/%m/%Y"))

#discerning negative vs. affirmative mentions (originally 2 was used to code negative mentions, e.g., "not establishment")
for (i in 1:length(pa.2016.dems.reshaped1$Value)){
  if (pa.2016.dems.reshaped1$Value[i] == 2){
    pa.2016.dems.reshaped1$Adjective[i] <- paste0("Not ", pa.2016.dems.reshaped1$Adjective[i])
    pa.2016.dems.reshaped1$Value[i] <- 1
  }
}

#reshape dataframe
pa.2016.dems.reshaped2.newspaper <- pa.2016.dems.reshaped1 %>%
  group_by(Adjective, Candidate) %>%
  dplyr::summarize(total = sum(Value)) %>%
  spread(key = Adjective, value = total) %>%
  replace(is.na(.), 0) %>%
  column_to_rownames(var = "Candidate") %>%
  as.data.frame(.)

#get percentages
pa.2016.dems.reshaped2.newspaper[1,] <- pa.2016.dems.reshaped2.newspaper[1,] / rowSums(pa.2016.dems.reshaped2.newspaper)[1]
pa.2016.dems.reshaped2.newspaper[2,] <- pa.2016.dems.reshaped2.newspaper[2,] / rowSums(pa.2016.dems.reshaped2.newspaper)[2]
pa.2016.dems.reshaped2.newspaper[3,] <- pa.2016.dems.reshaped2.newspaper[3,] / rowSums(pa.2016.dems.reshaped2.newspaper)[3]
for (i in 1:length(pa.2016.dems.reshaped2.newspaper)){
  pa.2016.dems.reshaped2.newspaper[,i] <- pa.2016.dems.reshaped2.newspaper[,i] * 100
}

print(xtable(t(pa.2016.dems.reshaped2.newspaper),
             digits=0,
             label="t:newspaper-pa-dems-2016", 
             caption="Distribution of adjectives used to describe Pennsylvania’s top candidates for the Democratic nomination to the Senate in 2016 in 95 articles from local (the Philadelphia Inquirer and the Pittsburgh Post-Gazette) and national (the Associated Press) outlets. Percentages reflect fraction of all adjectives used to describe each candidate."),
      file="tableA29.tex")#table A29

# table A30 ----- 
colnames(pa.2022.dems) <- gsub("\\.", "_", colnames(pa.2022.dems))
colnames(pa.2022.dems) <- gsub("__", "_", colnames(pa.2022.dems))
colnames(pa.2022.dems) <- gsub("Kenyatta__", "Kenyatta_", colnames(pa.2022.dems))
colnames(pa.2022.dems) <- gsub("Center_Left_", "Center Left_", colnames(pa.2022.dems))
colnames(pa.2022.dems) <- gsub("Far_Left_", "Far Left_", colnames(pa.2022.dems))
pa.2022.dems.reshaped <- pivot_longer(pa.2022.dems, cols = -c(Source, Date, Title), names_to = "Adjective", values_to = "Value")
pa.2022.dems.reshaped1 <- separate(pa.2022.dems.reshaped, col = c("Adjective"), into = c("Candidate", "Adjective", "Empty"), sep = "_") %>%
  dplyr::select(-Empty) %>%
  mutate(Date = as.Date(Date, format = "%d/%m/%Y"))

#discerning negative vs. affirmative mentions (originally 2 was used to code negative mentions, e.g., "not establishment")
for (i in 1:length(pa.2022.dems.reshaped1$Value)){
  if (pa.2022.dems.reshaped1$Value[i] == 2){
    pa.2022.dems.reshaped1$Adjective[i] <- paste0("Not ", pa.2022.dems.reshaped1$Adjective[i])
    pa.2022.dems.reshaped1$Value[i] <- 1
  }
}

#reshape dataframe
pa.2022.dems.reshaped2.newspaper <- pa.2022.dems.reshaped1 %>%
  group_by(Adjective, Candidate) %>%
  dplyr::summarize(total = sum(Value)) %>%
  spread(key = Adjective, value = total) %>%
  replace(is.na(.), 0) %>%
  column_to_rownames(var = "Candidate") %>%
  as.data.frame(.)

#get percentages
pa.2022.dems.reshaped2.newspaper[1,] <- pa.2022.dems.reshaped2.newspaper[1,] / rowSums(pa.2022.dems.reshaped2.newspaper)[1]
pa.2022.dems.reshaped2.newspaper[2,] <- pa.2022.dems.reshaped2.newspaper[2,] / rowSums(pa.2022.dems.reshaped2.newspaper)[2]
pa.2022.dems.reshaped2.newspaper[3,] <- pa.2022.dems.reshaped2.newspaper[3,] / rowSums(pa.2022.dems.reshaped2.newspaper)[3]
for (i in 1:length(pa.2022.dems.reshaped2.newspaper)){
  pa.2022.dems.reshaped2.newspaper[,i] <- pa.2022.dems.reshaped2.newspaper[,i] * 100
}

print(xtable(t(pa.2022.dems.reshaped2.newspaper),
             digits=0,
             label="t:newspaper-pa-dems-2022", 
             caption="Distribution of adjectives used to describe Pennsylvania’s top candidates for the Democratic nomination to the Senate in 2022 in 82 articles from local (the Philadelphia Inquirer and the Pittsburgh Post-Gazette) and national (the Associated Press) outlets. Percentages reflect fraction of all adjectives used to describe each candidate."),
      file="tableA30.tex")#table A30

# table A31 ----- 
colnames(pa.2022.gop) <- gsub("\\.", "_", colnames(pa.2022.gop))
colnames(pa.2022.gop) <- gsub("__", "_", colnames(pa.2022.gop))
colnames(pa.2022.gop) <- gsub("Center_right_", "Center Right_", colnames(pa.2022.gop))
colnames(pa.2022.gop) <- gsub("Far_Right_", "Far Right_", colnames(pa.2022.gop))
colnames(pa.2022.gop) <- gsub("Pro_Trump_", "Pro Trump_", colnames(pa.2022.gop))
colnames(pa.2022.gop) <- gsub("Anti_Trump_", "Anti Trump_", colnames(pa.2022.gop))
pa.2022.gop.reshaped <- pivot_longer(pa.2022.gop, cols = -c(Source, Date, Title), names_to = "Adjective", values_to = "Value")
pa.2022.gop.reshaped1 <- separate(pa.2022.gop.reshaped, col = c("Adjective"), into = c("Candidate", "Adjective", "Empty"), sep = "_") %>%
  dplyr::select(-Empty) %>%
  mutate(Date = as.Date(Date, format = "%d/%m/%Y"))

#discerning negative vs. affirmative mentions (originally 2 was used to code negative mentions, e.g., "not establishment")
for (i in 1:length(pa.2022.gop.reshaped1$Value)){
  if (pa.2022.gop.reshaped1$Value[i] == 2){
    pa.2022.gop.reshaped1$Adjective[i] <- paste0("Not ", pa.2022.gop.reshaped1$Adjective[i])
    pa.2022.gop.reshaped1$Value[i] <- 1
  }
}

#reshape dataframe
pa.2022.gop.reshaped2.newspaper <- pa.2022.gop.reshaped1 %>%
  group_by(Adjective, Candidate) %>%
  dplyr::summarize(total = sum(Value)) %>%
  spread(key = Adjective, value = total) %>%
  replace(is.na(.), 0) %>%
  column_to_rownames(var = "Candidate") %>%
  as.data.frame(.)

#get percentages
pa.2022.gop.reshaped2.newspaper[1,] <- pa.2022.gop.reshaped2.newspaper[1,] / rowSums(pa.2022.gop.reshaped2.newspaper)[1]
pa.2022.gop.reshaped2.newspaper[2,] <- pa.2022.gop.reshaped2.newspaper[2,] / rowSums(pa.2022.gop.reshaped2.newspaper)[2]
pa.2022.gop.reshaped2.newspaper[3,] <- pa.2022.gop.reshaped2.newspaper[3,] / rowSums(pa.2022.gop.reshaped2.newspaper)[3]
for (i in 1:length(pa.2022.gop.reshaped2.newspaper)){
  pa.2022.gop.reshaped2.newspaper[,i] <- pa.2022.gop.reshaped2.newspaper[,i] * 100
}

print(xtable(t(pa.2022.gop.reshaped2.newspaper),
             digits=0,
             label="t:newspaper-pa-gop-2022", 
             caption="Distribution of adjectives used to describe Pennsylvania’s top candidates for the Republican nomination to the Senate in 2022 in 146 articles from local (the Philadelphia Inquirer and the Pittsburgh Post-Gazette) and national (the Associated Press and Politico) outlets. Percentages reflect fraction of all adjectives used to describe each candidate."),
      file="tableA31.tex")#table A31

# table A32 ----- 
colnames(oh.2018.gop) <- gsub("\\.", "_", colnames(oh.2018.gop))
colnames(oh.2018.gop) <- gsub("__", "_", colnames(oh.2018.gop))
colnames(oh.2018.gop) <- gsub("Center_right_", "Center Right_", colnames(oh.2018.gop))
colnames(oh.2018.gop) <- gsub("Far_Right_", "Far Right_", colnames(oh.2018.gop))
colnames(oh.2018.gop) <- gsub("Pro_Trump_", "Pro Trump_", colnames(oh.2018.gop))
colnames(oh.2018.gop) <- gsub("Anti_Trump_", "Anti Trump_", colnames(oh.2018.gop))
oh.2018.gop.reshaped <- pivot_longer(oh.2018.gop, cols = -c(Source, Date, Title), names_to = "Adjective", values_to = "Value")
oh.2018.gop.reshaped1 <- separate(oh.2018.gop.reshaped, col = c("Adjective"), into = c("Candidate", "Adjective", "Empty"), sep = "_") %>%
  dplyr::select(-Empty) %>%
  mutate(Date = as.Date(Date, format = "%d/%m/%Y"))

#discerning negative vs. affirmative mentions (originally 2 was used to code negative mentions, e.g., "not establishment")
for (i in 1:length(oh.2018.gop.reshaped1$Value)){
  if (oh.2018.gop.reshaped1$Value[i] == 2){
    oh.2018.gop.reshaped1$Adjective[i] <- paste0("Not ", oh.2018.gop.reshaped1$Adjective[i])
    oh.2018.gop.reshaped1$Value[i] <- 1
  }
}

#reshape dataframe
oh.2018.gop.reshaped2.newspaper <- oh.2018.gop.reshaped1 %>%
  group_by(Adjective, Candidate) %>%
  dplyr::summarize(total = sum(Value)) %>%
  spread(key = Adjective, value = total) %>%
  replace(is.na(.), 0) %>%
  column_to_rownames(var = "Candidate") %>%
  as.data.frame(.)

#get percentages
oh.2018.gop.reshaped2.newspaper[1,] <- oh.2018.gop.reshaped2.newspaper[1,] / rowSums(oh.2018.gop.reshaped2.newspaper)[1]
oh.2018.gop.reshaped2.newspaper[2,] <- oh.2018.gop.reshaped2.newspaper[2,] / rowSums(oh.2018.gop.reshaped2.newspaper)[2]
oh.2018.gop.reshaped2.newspaper[3,] <- oh.2018.gop.reshaped2.newspaper[3,] / rowSums(oh.2018.gop.reshaped2.newspaper)[3]

for (i in 1:length(oh.2018.gop.reshaped2.newspaper)){
  oh.2018.gop.reshaped2.newspaper[,i] <- oh.2018.gop.reshaped2.newspaper[,i] * 100
}
print(xtable(t(oh.2018.gop.reshaped2.newspaper),digits=0))

print(xtable(t(oh.2018.gop.reshaped2.newspaper),
             digits=0,
             label="t:newspaper-oh-2018", 
             caption="Distribution of adjectives used to describe Ohio’s top candidates for the Republican nomination to the Senate in 2018 in 99 articles from local (the Plain Dealer, the Columbus Dispatch, and the Akron Beacon Journal ) and national (the Associated Press and Politico) newspapers. Percentages reflect fraction of all adjectives used to describe each can- didate."),
      file="tableA32.tex")#table A32

# table A33 ----- 
colnames(al.2017.gop) <- gsub("\\.", "_", colnames(al.2017.gop))
colnames(al.2017.gop) <- gsub("__", "_", colnames(al.2017.gop))
colnames(al.2017.gop) <- gsub("Center_right_", "Center Right_", colnames(al.2017.gop))
colnames(al.2017.gop) <- gsub("Far_Right_", "Far Right_", colnames(al.2017.gop))
colnames(al.2017.gop) <- gsub("Pro_Trump_", "Pro Trump_", colnames(al.2017.gop))
colnames(al.2017.gop) <- gsub("Anti_Trump_", "Anti Trump_", colnames(al.2017.gop))
al.2017.gop.reshaped <- pivot_longer(al.2017.gop, cols = -c(Source, Date, Title), names_to = "Adjective", values_to = "Value")
al.2017.gop.reshaped1 <- separate(al.2017.gop.reshaped, col = c("Adjective"), into = c("Candidate", "Adjective", "Empty"), sep = "_") %>%
  dplyr::select(-Empty) %>%
  mutate(Date = as.Date(Date, format = "%d/%m/%Y"))

#discerning negative vs. affirmative mentions (originally 2 was used to code negative mentions, e.g., "not establishment")
for (i in 1:length(al.2017.gop.reshaped1$Value)){
  if (al.2017.gop.reshaped1$Value[i] == 2){
    al.2017.gop.reshaped1$Adjective[i] <- paste0("Not ", al.2017.gop.reshaped1$Adjective[i])
    al.2017.gop.reshaped1$Value[i] <- 1
  }
}

#reshape dataframe
al.2017.gop.reshaped2.newspaper <- al.2017.gop.reshaped1 %>%
  group_by(Adjective, Candidate) %>%
  dplyr::summarize(total = sum(Value)) %>%
  spread(key = Adjective, value = total) %>%
  replace(is.na(.), 0) %>%
  column_to_rownames(var = "Candidate") %>%
  as.data.frame(.)

#get percentages
al.2017.gop.reshaped2.newspaper[1,] <- al.2017.gop.reshaped2.newspaper[1,] / rowSums(al.2017.gop.reshaped2.newspaper)[1]
al.2017.gop.reshaped2.newspaper[2,] <- al.2017.gop.reshaped2.newspaper[2,] / rowSums(al.2017.gop.reshaped2.newspaper)[2]
al.2017.gop.reshaped2.newspaper[3,] <- al.2017.gop.reshaped2.newspaper[3,] / rowSums(al.2017.gop.reshaped2.newspaper)[3]

for (i in 1:length(al.2017.gop.reshaped2.newspaper)){
  al.2017.gop.reshaped2.newspaper[,i] <- al.2017.gop.reshaped2.newspaper[,i] * 100
}
print(xtable(t(al.2017.gop.reshaped2.newspaper),digits=0))

print(xtable(t(al.2017.gop.reshaped2.newspaper),
             digits=0,
             label="t:newspaper-al-2017", 
             caption="Distribution of adjectives used to describe Alabama’s top candidates for the Republican nomination to the Senate in 2017 in 159 articles from local (Birmingham News) and national (the Associated Press and Politico) newspapers. Percentages reflect fraction of all adjectives used to describe each candidate."),
      file="tableA33.tex")#table A33

# table A34 ----- 
colnames(al.2020.gop) <- gsub("\\.", "_", colnames(al.2020.gop))
colnames(al.2020.gop) <- gsub("__", "_", colnames(al.2020.gop))
colnames(al.2020.gop) <- gsub("Center_right_", "Center Right_", colnames(al.2020.gop))
colnames(al.2020.gop) <- gsub("Far_Right_", "Far Right_", colnames(al.2020.gop))
colnames(al.2020.gop) <- gsub("Pro_Trump_", "Pro Trump_", colnames(al.2020.gop))
colnames(al.2020.gop) <- gsub("Anti_Trump_", "Anti Trump_", colnames(al.2020.gop))
al.2020.gop.reshaped <- pivot_longer(al.2020.gop, cols = -c(Source, Date, Title), names_to = "Adjective", values_to = "Value")
al.2020.gop.reshaped1 <- separate(al.2020.gop.reshaped, col = c("Adjective"), into = c("Candidate", "Adjective", "Empty"), sep = "_") %>%
  dplyr::select(-Empty) %>%
  mutate(Date = as.Date(Date, format = "%d/%m/%Y"))

#discerning negative vs. affirmative mentions (originally 2 was used to code negative mentions, e.g., "not establishment")
for (i in 1:length(al.2020.gop.reshaped1$Value)){
  if (al.2020.gop.reshaped1$Value[i] == 2){
    al.2020.gop.reshaped1$Adjective[i] <- paste0("Not ", al.2020.gop.reshaped1$Adjective[i])
    al.2020.gop.reshaped1$Value[i] <- 1
  }
}

#reshape dataframe
al.2020.gop.reshaped2.newspaper <- al.2020.gop.reshaped1 %>%
  group_by(Adjective, Candidate) %>%
  dplyr::summarize(total = sum(Value)) %>%
  spread(key = Adjective, value = total) %>%
  replace(is.na(.), 0) %>%
  column_to_rownames(var = "Candidate") %>%
  as.data.frame(.)

#get percentages
al.2020.gop.reshaped2.newspaper[1,] <- al.2020.gop.reshaped2.newspaper[1,] / rowSums(al.2020.gop.reshaped2.newspaper)[1]
al.2020.gop.reshaped2.newspaper[2,] <- al.2020.gop.reshaped2.newspaper[2,] / rowSums(al.2020.gop.reshaped2.newspaper)[2]
al.2020.gop.reshaped2.newspaper[3,] <- al.2020.gop.reshaped2.newspaper[3,] / rowSums(al.2020.gop.reshaped2.newspaper)[3]

for (i in 1:length(al.2020.gop.reshaped2.newspaper)){
  al.2020.gop.reshaped2.newspaper[,i] <- al.2020.gop.reshaped2.newspaper[,i] * 100
}
print(xtable(t(al.2020.gop.reshaped2.newspaper),digits=0))

print(xtable(t(al.2020.gop.reshaped2.newspaper),
             digits=0,
             label="t:newspaper-al-2020", 
             caption="Distribution of adjectives used to describe Alabama’s top candidates for the Republican nomination to the Senate in 2020 in 38 articles from local (Birmingham News) and national (the Associated Press and Politico) newspapers. Percentages reflect fraction of all adjectives used to describe each candidate."),
      file="tableA34.tex")#table A34

# table A35 ----- 
colnames(al.2022.gop) <- gsub("\\.", "_", colnames(al.2022.gop))
colnames(al.2022.gop) <- gsub("__", "_", colnames(al.2022.gop))
colnames(al.2022.gop) <- gsub("Center_right_", "Center Right_", colnames(al.2022.gop))
colnames(al.2022.gop) <- gsub("Far_Right_", "Far Right_", colnames(al.2022.gop))
colnames(al.2022.gop) <- gsub("Pro_Trump_", "Pro Trump_", colnames(al.2022.gop))
colnames(al.2022.gop) <- gsub("Anti_Trump_", "Anti Trump_", colnames(al.2022.gop))
al.2022.gop.reshaped <- pivot_longer(al.2022.gop, cols = -c(Source, Date, Title), names_to = "Adjective", values_to = "Value")
al.2022.gop.reshaped1 <- separate(al.2022.gop.reshaped, col = c("Adjective"), into = c("Candidate", "Adjective", "Empty"), sep = "_") %>%
  dplyr::select(-Empty) %>%
  mutate(Date = as.Date(Date, format = "%d/%m/%Y"))

#discerning negative vs. affirmative mentions (originally 2 was used to code negative mentions, e.g., "not establishment")
for (i in 1:length(al.2022.gop.reshaped1$Value)){
  if (al.2022.gop.reshaped1$Value[i] == 2){
    al.2022.gop.reshaped1$Adjective[i] <- paste0("Not ", al.2022.gop.reshaped1$Adjective[i])
    al.2022.gop.reshaped1$Value[i] <- 1
  }
}

#reshape dataframe
al.2022.gop.reshaped2.newspaper <- al.2022.gop.reshaped1 %>%
  group_by(Adjective, Candidate) %>%
  dplyr::summarize(total = sum(Value)) %>%
  spread(key = Adjective, value = total) %>%
  replace(is.na(.), 0) %>%
  column_to_rownames(var = "Candidate") %>%
  as.data.frame(.)

#get percentages
al.2022.gop.reshaped2.newspaper[1,] <- al.2022.gop.reshaped2.newspaper[1,] / rowSums(al.2022.gop.reshaped2.newspaper)[1]
al.2022.gop.reshaped2.newspaper[2,] <- al.2022.gop.reshaped2.newspaper[2,] / rowSums(al.2022.gop.reshaped2.newspaper)[2]
al.2022.gop.reshaped2.newspaper[3,] <- al.2022.gop.reshaped2.newspaper[3,] / rowSums(al.2022.gop.reshaped2.newspaper)[3]

for (i in 1:length(al.2022.gop.reshaped2.newspaper)){
  al.2022.gop.reshaped2.newspaper[,i] <- al.2022.gop.reshaped2.newspaper[,i] * 100
}
print(xtable(t(al.2022.gop.reshaped2.newspaper),
             digits=0,
             label="t:newspaper-al-2022", 
             caption="Distribution of adjectives used to describe Alabama’s top candidates for the Republican nomination to the Senate in 2022 in 71 articles from local (Birmingham News) and national (the Associated Press and Politico) newspapers. Percentages reflect fraction of all adjectives used to describe each candidate."),
      file="tableA35.tex")#table A35
